'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 叛军来袭战斗记录
 * @author sundbcn@gmail.com (sun debing)
 */

var moment = require('moment');
var mongoose = require('mongoose');
var _ = require('lodash');
var autoIncrement = require('mongoose-auto-increment');

/**
 * 叛军来袭战斗记录
 */
var rebelRecordSchema = new mongoose.Schema({
    // 角色id
    atk_role_id: {
        type: Number,
        required: true
    },

    // 来袭id
    rebel_id: {
        type: Number,
        required: true
    },

    // 防守方数据
    atk: {},

    // 攻击方数据
    def: {},

    // 通过的波数
    pass_wave: {
        type: Number
    },

    // 重生次数
    relive_num: {
        type: Number,
        default: 0
    },

    // 创建时间
    create_time: {
        type: Date,
        default: Date.now
    }
});

/**
 * 获取战斗记录的编号
 */
rebelRecordSchema.methods.getRecordId = function () {
    return this._id;
};

/**
 * 获取来袭id
 * @return {[type]} [description]
 */
rebelRecordSchema.methods.getRecordId = function () {
    return this._id;
};

/**
 * 获取攻击方的角色id
 * @return {[type]} [description]
 */
rebelRecordSchema.methods.getAtkRoleId = function () {
    return this.atk_role_id;
};

/**
 * 获取被攻击方的角色id
 * @return {[type]} [description]
 */
rebelRecordSchema.methods.getRebelId = function () {
    return this.rebel_id;
};

rebelRecordSchema.methods.setPassWave = function (pass_wave) {
    this.pass_wave = pass_wave;
};

/**
 * 判断战斗是否已经结束了
 * @return {Boolean} [description]
 */
rebelRecordSchema.methods.isEnd = function () {
    return this.pass_wave !== undefined;
};

/**
 * 添加重生的次数
 */
rebelRecordSchema.methods.addReliveNum = function () {
    this.relive_num++;
};

/**
 * 获取重生的次数
 * @return {[type]} [description]
 */
rebelRecordSchema.methods.getReliveNum = function () {
    return this.relive_num;
};

rebelRecordSchema.plugin(autoIncrement.plugin, {
    model: 'rebelRecordSchema',
    startAt: 10000
});

if (!rebelRecordSchema.options.toJSON) {
    rebelRecordSchema.options.toJSON = {};
}
/* jshint unused:false */
rebelRecordSchema.options.toJSON.transform = function (doc, ret) {
    ret.create_time = moment(ret.create_time).unix();
    if (ret.success !== undefined) {
        ret.success = +ret.success;
    }
    delete ret._id;
    delete ret.__v;
};

mongoose.model('RebelRecord', rebelRecordSchema);
